﻿using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UCL.Powersource;

namespace PowersourceUIToolkit.Test
{
    [TestClass]
    public class LogTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test Context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        LogManager DefaultLog;
        TraceListenerForTest traceListener;

        [TestInitialize]
        public void Setup()
        {
            DefaultLog = LogManager.DefaultInstance;
            traceListener = new TraceListenerForTest();
            Trace.Listeners.Add(traceListener);
            //DefaultTraceListener
        }
        [TestCleanup]
        public void Cleanup()
        {
            DefaultLog.Dispose();
        }

        [TestMethod]
        public void TestLog()
        {
            //int TestMessageIndex = 0;
            DefaultLog.Debug("Test Debug on {0}", 1);
            if (DefaultLog.VerbosityLevel >= VerbosityLevels.Debug)
            {
#if DEBUG
                Assert.AreEqual("Debug: Test Debug on 1", traceListener.LastMessage);
#endif
            }

            DefaultLog.Trace("Test Trace {0}", 1);
            Assert.AreEqual("Trace: Test Trace 1", traceListener.LastMessage);

            DefaultLog.Info("Test Info {0}", 1);
            Assert.AreEqual("Info: Test Info 1", traceListener.LastMessage);

            DefaultLog.Warn("Test Warning {0}", 1);
            Assert.AreEqual("Warning: Test Warning 1", traceListener.LastMessage);

            var exc=new Exception("TestException");

            DefaultLog.Warn(exc, "Test Warning {0} - Exception", 2);
            Assert.AreEqual("Warning: Test Warning 2 - Exception " + exc.ToString(), traceListener.LastMessage);

            DefaultLog.Warn(null, "Test Warning {0} - Null Exception", 3);
            Assert.AreEqual("Warning: Test Warning 3 - Null Exception", traceListener.LastMessage);

            DefaultLog.Error(exc,"Test Error {0}",1);
            Assert.AreEqual("Error: Test Error 1 "+exc.ToString(), traceListener.LastMessage);

            DefaultLog.Error(null, "Test Error {0} - Null Exception", 2);
            Assert.AreEqual("Error: Test Error 2 - Null Exception", traceListener.LastMessage);

            DefaultLog.Trace("Simple trace log message");
            Assert.AreEqual("Trace: Simple trace log message", traceListener.LastMessage);
        }

        
    }
}
